package com.ruoyi.web.controller.system;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.system.domain.MktReward;
import com.ruoyi.system.util.PhoneUtil;
import com.ruoyi.system.domain.vo.MktActivityVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MktActivity;
import com.ruoyi.system.service.IMktActivityService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/marketing/activity")
public class MktActivityController extends BaseController
{
    @Autowired
    private IMktActivityService mktActivityService;
    /**
     * 查询营销活动管理列表
     */
    @PreAuthorize("@ss.hasPermi('marketing:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(MktActivity mktActivity)
    {
        startPage();
        List<MktActivity> list = mktActivityService.selectMktActivityList(mktActivity);
        return getDataTable(list);
    }

    /**
     * 导出营销活动管理列表
     */
    @PreAuthorize("@ss.hasPermi('marketing:activity:export')")
    @Log(title = "营销活动管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MktActivity mktActivity)
    {
        List<MktActivity> list = mktActivityService.selectMktActivityList(mktActivity);
        ExcelUtil<MktActivity> util = new ExcelUtil<MktActivity>(MktActivity.class);
        util.exportExcel(response, list, "营销活动管理数据");
    }

    /**
     * 获取营销活动管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('marketing:activity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(mktActivityService.getById(id));
    }

    /**
     * 新增营销活动管理
     */
    @PreAuthorize("@ss.hasPermi('marketing:activity:add')")
    @Log(title = "营销活动管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MktActivityVo mktActivity)
    {
        mktActivityService.save(mktActivity);
        return AjaxResult.success("保存成功");
    }

    /**
     * 修改营销活动管理
     */
    @PreAuthorize("@ss.hasPermi('marketing:activity:edit')")
    @Log(title = "营销活动管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MktActivityVo mktActivity)
    {

        mktActivityService.updateById(mktActivity);
        return AjaxResult.success("修改成功");

    }

    /**
     * 删除营销活动管理
     */
    @PreAuthorize("@ss.hasPermi('marketing:activity:remove')")
    @Log(title = "营销活动管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mktActivityService.removeBatchByIds(Arrays.asList(ids)));
    }

    @PreAuthorize("@ss.hasPermi('marketing:activity:upload')")
    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file){

        // String path = PhoneUtil.uploadFile("test", file);
        return AjaxResult.success();
    }


    /**
     * 用户端完成抽奖接口
     * @param actId
     * @return
     */
    @PostMapping("/lottery")
    public AjaxResult lottery(Long actId){

        //获取抽奖人的信息
        LoginUser loginUser = getLoginUser();

        //调用方法完成抽奖
        MktReward mktReward = mktActivityService.lottery(actId,loginUser);
        return AjaxResult.success(mktReward);
    }
}